package wowraidmanager.server.service.wishlist;

import java.util.ArrayList;

import org.slim3.datastore.Datastore;

import wowraidmanager.client.service.wishlist.MemberService;
import wowraidmanager.server.meta.wishlist.MemberMeta;
import wowraidmanager.shared.model.wishlist.Member;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class MemberServiceImpl implements MemberService {
    MemberMeta query = MemberMeta.get();

    public void setMember(Member member){
        Datastore.put(member);
    }

    public void setMember(ArrayList<Member> memberList){
        Datastore.put(memberList);
    }

    public ArrayList<Member> getMember(String memberAccount){
        return (ArrayList<Member>)Datastore.query(query).filter(query.memberAccount.equal(memberAccount)).asList();
    }

    public ArrayList<Member> getAllMember(){

        return (ArrayList<Member>)Datastore.query(query).sort(query.guild.asc, query.realm.asc,query.order.asc).asList();
    }

    public ArrayList<Member> getAllMember(Member currentMember){
        return (ArrayList<Member>)Datastore.query(query).filter(query.guild.equal(currentMember.getGuild()),query.realm.equal(currentMember.getRealm())).sort(query.guild.asc, query.realm.asc,query.order.asc).asList();
    }

    public Member getMemberbyKey(String targetUser){

        Key targetKey = getKeyById(targetUser);
        return Datastore.query(query).filter(query.key.equal(targetKey)).asSingle();
    }

    public void deleteMemberbyKey(String targetUser){
        Key targetKey = getKeyById(targetUser);
        Datastore.delete(targetKey);
    }

    public Key getKeyById(String targetUser){
        String[] targetUserSplit = targetUser.split("\\(");
        String kind = targetUserSplit[0];
        long id = Long.valueOf(targetUserSplit[1].substring(0, targetUserSplit[1].length() - 1));


        return KeyFactory.createKey(kind, id);
    }
}
